package com.tgy.reflection;

import java.lang.annotation.*;
import java.lang.reflect.Field;

/***
 * @ClassName: ReflDoAnno
 * @Description: 练习反射操作注解
 * @Auther: 送一一块二向箔
 * @Date: 23:09 2021/12/1
 * @version : V1.0
 */
public class ReflDoAnno {
    public static void main(String[] args) throws NoSuchFieldException {
        Class<School> c1 = School.class;
        //通过反射获得注解
        Annotation[] annotations = c1.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println(annotation);
        }
        //获得注解的value值
        TableTang tableTang = (TableTang)c1.getAnnotation(TableTang.class);
        String value = tableTang.value();
        System.out.println(value);

        //获得类指定注解
        Field names = c1.getDeclaredField("name");
        FieldTang annotation = names.getAnnotation(FieldTang.class);
        System.out.println(annotation.columnName());
        System.out.println(annotation.length());
        System.out.println(annotation.type());
    }
}
@TableTang("db_school")
class School{
    @FieldTang(columnName = "Fi_name",type ="varchar",length = 10)
    private String name;
    @FieldTang(columnName = "Fi_id",type ="int",length = 1)
    private int id;

    public School() {
    }

    public School(String name, int id) {
        this.name = name;
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public School setName(String name) {
        this.name = name;
        return this;
    }

    public int getId() {
        return id;
    }

    public School setId(int id) {
        this.id = id;
        return this;
    }

    @Override
    public String toString() {
        return "School{" +
                "name='" + name + '\'' +
                ", id=" + id +
                '}';
    }
}

//类名的注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface TableTang{
    String value();
}
//属性的注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface FieldTang{
    String columnName();
    String type();
    int length();
}
